import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

@WebServlet("/registerServlet")
public class register extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        doGet(request,response);

    }
    public void wrong1(){
        String msg="不允许有空，注册失败！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="信息提示";
        JOptionPane.showMessageDialog(null, msg, title, type);
    }
    public void wrong2(){
        String msg="两次密码不同，注册失败！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="信息提示";
        JOptionPane.showMessageDialog(null, msg, title, type);
    }
    public void wrong3(){
        String msg="用户名已存在，注册失败！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="信息提示";
        JOptionPane.showMessageDialog(null, msg, title, type);
    }
    public void right(){
        String msg="注册信息合格，注册成功！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="信息提示";
        JOptionPane.showMessageDialog(null, msg, title, type);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String userName=new String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8");
        String password1=new String(request.getParameter("password").getBytes("ISO-8859-1"),"UTF-8");
        String password2=new String(request.getParameter("password2").getBytes("ISO-8859-1"),"UTF-8");
        String email=new String(request.getParameter("email").getBytes("ISO-8859-1"),"UTF-8");
        String phone=new String(request.getParameter("phone").getBytes("ISO-8859-1"),"UTF-8");
        String question=new String(request.getParameter("question").getBytes("ISO-8859-1"),"UTF-8");
        String answer=new String(request.getParameter("answer").getBytes("ISO-8859-1"),"UTF-8");
        if(userName.length()==0||password1.length()==0||password2.length()==0||phone.length()==0||email.length()==0){
            wrong1();
            response.sendRedirect("http://localhost:8084/PIMS/register/register.jsp");
        }else if(!(password1.equals(password2))){
            wrong2();
            response.sendRedirect("http://localhost:8084/PIMS/register/register.jsp");
        }else{
            try{
                Connection con=null;
                ResultSet rs=null;
                Statement stmt=null;

                Class.forName("org.h2.Driver");
                /*url后面加的?useUnicode=true&characterEncoding=gbk，是为了处理向数据库中添加数据时出现乱码的问题。*/
                String url="jdbc:h2:~/test1";
                con= DriverManager.getConnection(url,"root","root");
                stmt=con.createStatement();
                String sql1="select * from shop_user where username='"+userName+"'";
                rs=stmt.executeQuery(sql1);
                rs.last();
                int k;
                k=rs.getRow();
                if(k>0){
                    wrong3();
                    response.sendRedirect("http://localhost:8084/PIMS/register/register.jsp");
                }else{
                    String sql2="insert into shop_user"+"(use_id,userName,password,phone,img,sex,role,address)"+"values("+"'"+userName+"'"+","+"'"+password1+"'"+","+"'"+email+"'"+","+"'"+phone+"'"+","+"'"+question+"'"+","+"'"+answer+"'"+","+"1"+")";
                    stmt.executeUpdate(sql2);
                }
                rs.close();
                stmt.close();
                con.close();
                right();
                response.sendRedirect("http://localhost:8084/PIMS/login.jsp");
            }catch(Exception e){
                e.printStackTrace();
            }
        }

    }
}

